//
//  VulamLevelView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/16.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class VulamLevelView: NSView {
    
    var uViews:[NSView] = []
    
    // 0-10
    var level:Int = 0 {
        didSet {
            self.setVulam(level: level)
        }
    }
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVulam(level:Int) {
        for (_,val) in self.uViews.enumerated() {
            val.layer?.backgroundColor = color_gray(220).cgColor
        }
        
        if level > 0 && level <= 10 {
            let newViews = self.uViews[0..<level]
            for (_,val) in newViews.enumerated() {
                val.layer?.backgroundColor = color_green200.cgColor
            }
        }
    }
    
    func setupUI() {
        self.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.uViews = self.initViews(inView: self.container)
    }
    
    lazy var container: NSView = {
        let view = NSView()
        return view
    }()
    
    func initViews(inView:NSView) -> [NSView] {
        let kWidth:CGFloat = 300
        let space:CGFloat = 1
        let width:CGFloat = (kWidth-space*9)/10
        var uViews:[NSView] = []
        for inx in 0..<10 {
            let colorView = NSView()
            colorView.wantsLayer = true
            colorView.layer?.backgroundColor = color_gray(220).cgColor
            colorView.layer?.cornerRadius = 5
            colorView.layer?.masksToBounds = true
            
            inView.addSubview(colorView)
            uViews.append(colorView)
            colorView.snp.makeConstraints { (make) in
                make.width.equalTo(width)
                make.height.equalTo(width*6/10)
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(uViews[inx-1].snp.right).offset(1)
                }
                
            }
        }
        return uViews
    }
}
